-- Channel Mod
-- Better-looking list

-- ChannelList

local PANEL = {}

local ICON_CURRENT = surface.GetTextureID( "gui/silkicons/check_on" )
local ICON_UNLOCKED = surface.GetTextureID( "gui/silkicons/group" )
local ICON_LOCKED = surface.GetTextureID( "gui/silkicons/shield" )

function PANEL:AddChannel( Name, ID, Locked )
	local Item = self:AddItem( "" ) -- Blank
	Item.Name = Name
	Item.ID = ID
	Item.Locked = Locked
	Item:SetTall(23)
	
	-- Change the look
	function Item:PaintOver()
		if Channels:GetCurrent() == self.ID then
			surface.SetDrawColor( 0, 210, 255, 255 )
			surface.DrawOutlinedRect( 0, 0, self:GetWide() - 1, self:GetTall() - 1 )
			
			surface.SetTexture( ICON_CURRENT )
		elseif self.Locked then
			surface.SetTexture( ICON_LOCKED )
		else
			surface.SetTexture( ICON_UNLOCKED )
		end
		
		surface.SetDrawColor( 255, 255, 255, 255 )
		surface.DrawTexturedRect( 8, self:GetTall() / 2 - 8, 16, 16 )
		
		draw.SimpleText( self.Name, "Default", 29, self:GetTall() / 2, Color( 0, 0, 0, 255 ), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER )
	end
	
	Item.OldSetSelected = Item.SetSelected
	function Item:SetSelected( bSelected ) -- BO SELECTED!
		if not self.Locked then return self:OldSetSelected( bSelected ) end
		
		if bSelected then
			self.Passbox = vgui.Create( "ChannelEntry", self )
			self.Passbox:SetWide( 150 )
			self.Passbox:SetPos( self:GetWide() - self.Passbox:GetWide() - 5, 1 )
			self.Passbox:SetText( self.Password or "" )
			
			self.PassLabel = vgui.Create( "DLabel", self )
			self.PassLabel:SetPos( self.Passbox:GetPos() - 55, 1 )
			self.PassLabel:SetText( "Password:" )
			self.PassLabel:SetTextColor( Color( 0, 0, 0, 255 ) )
			
		elseif self.Passbox then
			self.Password = self.Passbox:GetValue()
			self.Passbox:Remove()
			self.Passbox = nil
			
			self.PassLabel:Remove()
		end
		
		self:OldSetSelected( bSelected )
	end
	
	-- And now, add right mouse click
	Item.OldOnMousePressed = Item.OnMousePressed
	function Item:OnMousePressed( Button )
		if Button == MOUSE_RIGHT then
			self:Select()
			
			local Menu = DermaMenu()
			
			if Channels:GetCurrent() ~= self.Name then
				local Option = vgui.Create( "DMenuOption", Menu )
				Option:SetText( "Join channel" )
				Option:SetImage( "gui/silkicons/wrench" )
				Option:SetTall( 25 )
				Option.ID = self.ID
				
				if self.Passbox then
					Option.Password = self.Passbox:GetValue()
				else
					Option.Password = self.Password or ""
				end
				
				function Option:DoClick()
					RunConsoleCommand( "chan_join", self.ID, self.Password )
				end
				
				Option.OldPerformLayout = Option.PerformLayout
				function Option:PerformLayout()
					self:OldPerformLayout()
					
					self:SetTall( 23 )
					
					self.m_Image:Center()
					self.m_Image:AlignLeft( 4 )
					
					self:SetTextInset( self.m_Image:GetWide() + 8, 0 )
				end
				
				Menu:AddPanel( Option )
			else
				local Option = vgui.Create( "DMenuOption", Menu )
				Option:SetText( "Leave channel" )
				Option:SetImage( "gui/silkicons/exclamation" )
				Option:SetTall( 25 )
				Option.ID = self.ID
				
				function Option:DoClick()
					RunConsoleCommand( "chan_leave", self.ID, self.Password )
				end
				
				Menu:AddPanel( Option )
			end
			
			Menu:Open()
		end
		self:OldOnMousePressed( Button )
	end
end

vgui.Register( "ChannelList", PANEL, "DComboBox" )



-- ChannelEntry

local PANEL = {}

function PANEL:PaintOver()
	if not self:HasFocus() then return end
	surface.SetDrawColor( 0, 180, 255, 255 )
	surface.DrawOutlinedRect( 1, 1, self:GetWide() - 2, self:GetTall() - 2 )
end

vgui.Register( "ChannelEntry", PANEL, "DTextEntry" )
